#!/bin/bash
set -e -x

cd $(dirname $0)/..

if [ -z "$K3S_IMAGE" ]; then
    source $(dirname $0)/version.sh
    TAG=${TAG:-${VERSION}${SUFFIX}}
    REPO=${REPO:-rancher}
    IMAGE_NAME=${IMAGE_NAME:-k3s}
    export K3S_IMAGE=${REPO}/${IMAGE_NAME}:${TAG}
fi

OUTPUT=$(pwd)/dist/artifacts
mkdir -p ${OUTPUT}

pids=()
output=()

run-sonobuoy() {
    output+=(${log_output})
    E2E_LOG_OUTPUT=${log_output} ./scripts/sonobuoy ${@} &
    pids+=($!)
}

log_output=${OUTPUT}/e2e-STATUS-${ARCH}-serial.log \
    run-sonobuoy --e2e-focus='\[Serial\].*\[Conformance\]'

log_output=${OUTPUT}/e2e-STATUS-${ARCH}-parallel.log \
    run-sonobuoy --e2e-focus='\[Conformance\]' --e2e-skip='\[Serial\]' --e2e-parallel=y

cleanup() {
    exit_status=$?
    set +e
    echo "Waiting on the tests!"
    wait
    exit ${exit_status}
}
trap cleanup EXIT
wait ${pids[@]}
trap - EXIT
